// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

// 云函数入口函数
exports.main = async (event, context) => {
  console.log('testArtPosts 云函数被调用，参数:', event);
  
  try {
    const wxContext = cloud.getWXContext()
    const openid = wxContext.OPENID
    
    console.log('测试云函数 - wxContext:', wxContext)
    console.log('测试云函数 - openid:', openid)
    
    // 测试数据库连接
    const db = cloud.database()
    console.log('数据库连接成功')
    
    // 测试集合访问
    const artPostsCollection = db.collection('artPosts')
    console.log('artPosts集合访问成功')
    
    return {
      success: true,
      message: '测试成功',
      openid: openid,
      event: event,
      timestamp: new Date().toISOString()
    }
  } catch (error) {
    console.error('testArtPosts 错误:', error)
    return {
      success: false,
      message: '测试失败',
      error: error.message || error
    }
  }
} 